package cn.iocoder.yudao.module.famous.doctor.controller.app.certificate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.certificate.vo.*;
import cn.iocoder.yudao.module.famous.doctor.convert.certificate.FamCertificateConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.certificate.FamCertificateDO;
import cn.iocoder.yudao.module.famous.doctor.service.certificate.FamCertificateService;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberDockerTjPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberDockerInfoRespVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberGetDockerInfoRespVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 互联网备案")
@RestController
@RequestMapping("/famous.doctor/fam-certificate/app")
@Validated
@Slf4j
public class AppFamCertificateController {

    @Resource
    private FamCertificateService famCertificateService;
    @Resource
    private MemberUserService userService;
    @PostMapping("/create")
    @Operation(summary = "创建互联网备案")
    public CommonResult<Long> createFamCertificate(@Valid @RequestBody FamCertificateCreateReqVO createReqVO) {
        return success(famCertificateService.createFamCertificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新互联网备案")
    public CommonResult<Boolean> updateFamCertificate(@Valid @RequestBody FamCertificateUpdateReqVO updateReqVO) {
        famCertificateService.updateFamCertificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除互联网备案")
    public CommonResult<Boolean> deleteFamCertificate(@RequestParam("id") Long id) {
        famCertificateService.deleteFamCertificate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得互联网备案")
    public CommonResult<FamCertificateRespVO> getFamCertificate(@RequestParam("id") Long id) {
        FamCertificateDO famCertificate = famCertificateService.getFamCertificate(id);
        return success(FamCertificateConvert.INSTANCE.convert(famCertificate));
    }

    @GetMapping("/getLoginCertificate")
    @Operation(summary = "获取登录用户互联网备案")
    public CommonResult<FamCertificateRespVO> getLoginCertificate() {
        FamCertificateDO famCertificate = famCertificateService.getLoginCertificate(getLoginUserId());
        FamCertificateRespVO convert = FamCertificateConvert.INSTANCE.convert(famCertificate);
        log.info("FamCertificateRespVO:{}"+convert);
        return success(convert);
    }

    /**
     * 推荐医生
     * @param
     * @return
     */
    @GetMapping("/select-docker")
    @Operation(summary = "推荐医生", description = "推荐医生-- illnessIds疾病标签 有值:给患者推荐对应医生 空值:查询所有标签医生")
    public CommonResult<PageResult<AppMemberDockerInfoRespVO>> selectTjDocker(@Valid MemberDockerTjPageReqVO pageVO) {
        PageResult<MemberUserDO> user = userService.selectTjDocker(pageVO);
        PageResult<AppMemberDockerInfoRespVO> result = MemberUserConvert.INSTANCE.convertDocker(user);
        result.getList().forEach(appMemberDockerInfoRespVO -> {
            FamCertificateDO loginCertificate = famCertificateService.getLoginCertificate(appMemberDockerInfoRespVO.getId());
            if(loginCertificate!=null){
                appMemberDockerInfoRespVO.setDepartment(loginCertificate.getDepartment());
                appMemberDockerInfoRespVO.setHospitalName(loginCertificate.getHospitalName());
            }
        });

        return success(result);
    }

    @GetMapping("/get-docker")
    @Operation(summary = "权威医生详情", description = "权威医生详情")
    public CommonResult<AppMemberGetDockerInfoRespVO> docker(@RequestParam("id") Long id) {

        MemberUserDO user = userService.selectById(id);
        FamCertificateDO loginCertificate = famCertificateService.getLoginCertificate(user.getId());
        AppMemberGetDockerInfoRespVO result = null;
        result = MemberUserConvert.INSTANCE.convertGetDocker(user);
        if (loginCertificate!=null) {
            BeanUtils.copyProperties(loginCertificate, result);
        }
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获得互联网备案列表")
    public CommonResult<List<FamCertificateRespVO>> getFamCertificateList(@RequestParam("ids") Collection<Long> ids) {
        List<FamCertificateDO> list = famCertificateService.getFamCertificateList(ids);
        return success(FamCertificateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得互联网备案分页")
    public CommonResult<PageResult<FamCertificateRespVO>> getFamCertificatePage(@Valid FamCertificatePageReqVO pageVO) {
        PageResult<FamCertificateDO> pageResult = famCertificateService.getFamCertificatePage(pageVO);
        return success(FamCertificateConvert.INSTANCE.convertPage(pageResult));
    }



}
